﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services;
using Meqs.Entity;
using Meqs.Logic.Interface;

public partial class EduLogin : BasePage
{
    protected string options;
    protected string schoolMark = string.Empty;
    protected int loginway = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GlobalApplication.Reged)
        //{
        //    //转向提示用户注册页面
        //    Response.Redirect("regErrorPage.htm");
        //    return;
        //}

        if (!IsPostBack)
        {
            BiuldSchoolAreas();



            if (Request.Cookies["SchoolMark"] != null)
            {
                schoolMark = Request.Cookies["SchoolMark"].Value.ToString();

            }

            if (Request.Cookies["LoginWay"] != null)
            {
                loginway = int.Parse(Request.Cookies["LoginWay"].Value); ;
            }
            //if (CheckReg())
            //{

            //    BiuldSchoolAreas();
            //}
            //else
            //{
            //    Response.Redirect("register.aspx");
            //}


        }

    }
    /// <summary>
    /// 判断是否可以使用
    /// </summary>
    /// <returns></returns>
    private bool CheckReg()
    {
        if (!GlobalApplication.Reged)
        {
            //获取注册对象
            IRegistrationLogic rlogic = Container<IRegistrationLogic>.Instance;
            IList<Registration> l = rlogic.FindAll();
            if (l != null && l.Count > 0)
            {
                try
                {
                    //比对加密字符结果
                    UserSerial u = new UserSerial(l[0].Data);
                    if (LoginCommon.Encrypt(u.SNum, LoginCommon.GetMacAddress()) == Convert.ToString(u.UserSecret))
                    {
                        //已经注册成功
                        GlobalApplication.Reged = true;
                        return true;
                    }
                    else
                    {
                        //没有注册
                        if (u.UsedNumber > 0)
                        {
                            //使用次数没到.可以使用
                            return true;
                        }
                    }
                }
                catch { }
            }
            return false;
        }
        return true;
    }
    private void BiuldSchoolAreas()
    {

        ISchoolAreaLogic schoolAreaLogic = Container<ISchoolAreaLogic>.Instance;
        List<SchoolArea> list = (List<SchoolArea>)schoolAreaLogic.FindAll();
        StringBuilder sb = new StringBuilder();
        int areaID = 0;
        if (Request.Cookies["Area"] != null)
        {
            areaID = int.Parse(Request.Cookies["Area"].Value);
        }


        sb.Append("<option value=\"0\">教育局</option>");
        //IList<SchoolArea> list= schoolAreaLogic.FindAll();
        foreach (SchoolArea area in list)
        {
            if (areaID == area.RID)
            {
                sb.Append("<option  value=\"" + area.RID.ToString() + "\" selected>&nbsp;&nbsp;" + area.SchoolAreaName + "</option>");
            }
            else
            {

                sb.Append("<option value=\"" + area.RID.ToString() + "\">&nbsp;&nbsp;" + area.SchoolAreaName + "</option>");

            }
        }
        options = sb.ToString();
    }
    /// <summary>
    /// 教师登陆
    /// </summary>
    /// <param name="schoolMark"></param>
    /// <param name="code"></param>
    /// <param name="pwd"></param>
    /// <returns></returns>
    [WebMethod]
    public static int TeaLogin(string schoolMark, string code, string pwd)
    {

        ITeacherInfoLogic studentInfoLogic = Container<ITeacherInfoLogic>.Instance;
        return studentInfoLogic.CheckLogin(schoolMark, code, pwd);
    }

    /// <summary>
    /// 学生登陆
    /// </summary>
    /// <param name="schoolMark"></param>
    /// <param name="code"></param>
    /// <param name="pwd"></param>
    /// <returns></returns>
    [WebMethod]
    public static int StuLogin(string schoolMark, string code, string pwd)
    {
        IStudentInfoLogic studentInfoLogic = Container<IStudentInfoLogic>.Instance;
        return studentInfoLogic.CheckLogin(schoolMark, code, pwd);

        //CheckLogin(string schoolMark, string stuCode, string pass)
        //return 1;

    }
    /// <summary>
    /// 局级登陆
    /// </summary>
    /// <param name="schoolArea"></param>
    /// <param name="userName"></param>
    /// <param name="pwd"></param>
    /// <returns></returns>
    [WebMethod]
    public static int EducationLogin(int schoolArea, string userName, string pwd)
    {
        IEducationUsersLogic educationUsersLogic = Container<IEducationUsersLogic>.Instance;
        int status = educationUsersLogic.CheckLogin(userName, pwd, schoolArea);
        if (status == 0)
        {
            if (!GlobalApplication.Reged)
            {
                IList<Registration> l = Container<IRegistrationLogic>.Instance.FindAll();
                if (l != null && l.Count > 0)
                {
                    try
                    {
                        //比对加密字符结果
                        UserSerial u = new UserSerial(l[0].Data);
                        //使用次数减一
                        if (u.UsedNumber > 0)
                        {
                            u.UsedNumber = u.UsedNumber - 1;
                            l[0].Data = u.GetArray();
                            Container<IRegistrationLogic>.Instance.Update(l[0]);
                        }
                    }
                    catch { }
                }
            }
        }
        return status;
    }
    
}
